<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\CommonModel;

class CommonController extends Controller {

    /**
     * 模板变量
     * @var array
     */
    public $_data = array();

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        //$this->Common = D('Menu');
        $this->_data['userInfo'] = session('userInfo');
    }

    /**
     * 生成验证码
     * @author mmy
     */
    public function verify()
    {
        $config = [
            'fontSize' => 19, // 验证码字体大小
            'length' => 4, // 验证码位数
            'imageH' => 34
        ];
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    /**
     * 验证码校验
     * @author mmy
     *
     */
    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        $res = $verify->check($code, $id);
        return $res;
    }

    /**
     * 检查用户是否登录
     * @author mmy
     */
    public function checkLogin()
    {
        $userInfo = session('userInfo');
        if($userInfo['isLogin'])
        {
            return true;
        }
        return false;
    }

    /**
     * Alert show message.
     *
     * @param string $message
     * @param string $mode
     * @return void
     */
    function showMessage($message, $mode = "back")
    {
        ob_start();
        if($mode == "back")
        {
            $cmd = "history.go(-1)";
        }
        else
        {
            $cmd = "location.href = '" . $mode . "';";
        }

        echo '<script language="javascript">
		<!--
			alert("' . $message . '");' . $cmd . '
		-->
			</script>';

        ob_end_flush();
        exit();
    }

    /**
     * 分页方法
     * @param $url
     * @param $param  url参数
     * @param $limit
     * @param $page
     * @param $total
     * @return bool|string
     */
    function Pagebarht($url, $param, $limit, $page, $total) {
        $str_html = '';
        if ($total < 0) {
            return false;
        }
        if ($url == "") {
            return false;
        }
        $link = $url . "&";
        if (is_array ( $param )) {
            foreach ( $param as $str_key => $str_value ) {
                $link = $link . "$str_key=" . urlencode ( $str_value ) . "&";
            }
        }
        $int_pages = ceil ( $total / $limit );
        if ($page < 1) {
            $page = 1;
        }
        if ($page > $int_pages) {
            $page = $int_pages;
        }
        $start_url = $link . "page=1";
        $end_url = $link . "page=$int_pages";
        $pre_url = $link . "page=" . ($page - 1);
        $next_url = $link . "page=" . ($page + 1);
        if ($page < 6) {
            $start_page = 1;
            $end_page = 7;
        } else {
            $start_page = $page - 5;
            $end_page = $page + 1;
        }
        if ($end_page > $int_pages) {
            $end_page = $int_pages;
        }
        $urls = null;
        /**
         * THE URL
         */
        for($i = $start_page, $j = 0; $i <= $end_page; $i ++, $j ++) {
            $temp_url = $link . "page=$i";
            if ($i == $page) {
                $urls [$j] = "<font class='psel'>" . $i . "</font>";
            } else {
                $urls [$j] = "<a href=\"$temp_url\">" . $i . "</a>";
            }
        }
        if (is_array ( $urls )) {
            $str_html = "<script>function doSubmit(value){if(isNaN(value)){value = 1;} var totPage=$int_pages;if(value>totPage){value = totPage;}if(value<1){value = 1;}window.location.href='$url&page='+value;}</script>";
            $str_html .= "<div id='pagerf' class='pager' ><span>";
            $str_html .= "<em>共" . $total . "条信息&nbsp;&nbsp;</em>";
            $str_html .= "&nbsp;<a class='pager' href=\"$start_url\" text=\回第1页\ >首页</a>&nbsp;&nbsp;";
            if ($page > 1) {
                $str_html .= "<a href=\"$pre_url\" class='pager'>上页</a>&nbsp;&nbsp;";
            } else {
                $str_html .= "<font class='page_up'>上页</font>&nbsp;&nbsp;";
            }
            foreach ( $urls as $sub_url ) {
                $str_html .= $sub_url;
            }
            if ($page >= $int_pages) {
                $str_html .= "<font class='page_down blue'>下页</font>&nbsp;&nbsp;";
            } else {
                $str_html .= "<a href=\"$next_url\" class='page_down blue'>下页</a>&nbsp;&nbsp";
            }
            $str_html .= "<a href=\"$end_url\" text=\"到第$int_pages\">尾页</a> &nbsp;&nbsp;";
            $str_html .= "<em>共" . $int_pages . "页 &nbsp;</em>";
            $str_html .= "<em>跳到 <input type=text class=ht_inpp size=4 onBlur='doSubmit(this.value)' /> 页 </em>";
            $str_html .= "<a href='javascript:void(0)'>确定</a>";
            $str_html .= "</span></div>";
            return $str_html;
        }
        return false;
    }

     /**
     * 设置用户session
     * @author mmy
     */
    public function setSession($arr)
    {
        if(count($arr)>0)
        {
            $array = array(
                'id'=>$arr[0]['id'],
                'name'=>$arr[0]['name'],
                'pass'=>$arr[0]['pass'],
                'age'=>$arr[0]['age'],
                'status'=>$arr[0]['status'],
                'isLogin'=>1,
            );

            session('userInfo',$array);
        }
    }

    /**
     * 获取导航方法
     * @author mmy
     */
    public function getMenuList(&$data,$index,$sub, $third = false)
    {
        $menuList = $this->Common->getMenuList();
        foreach($menuList as $key=>$val)
        {
            $data['mainMenu'][] = array(
                'name'=>$val['name'],
                'link'=>$val['link'],
                'class'=>$index==$key ? 'select_a':'',
                'target'=>$val['target'],
            );
            $index==$key ?$data['location_top'] = $val['name']:'首页';
            if($index==$key)
            {
                if($third===false&&!empty($val['children']))
                {
                    foreach($val['children'] as $k=>$subMenu)
                    {
                        $data['subMenu'][] = array(
                            'name'=>$subMenu['name'],
                            'link'=>$subMenu['link'],
                            'class'=>$sub==$k ? 'selectMenu':'',
                            'target'=>$subMenu['target'],
                        );
                        $sub==$k ?$data['location_next'] = $subMenu['name']:'管理';
                    }
                }
            }

        }
        unset($menuList) ;

    }

    /**
     * 退出登陆方法
     * @author mmy
     */
    public function logout()
    {
        session_destroy();
        $loginController = A('Login');
        $loginController->index();
    }
}